package com.zishuimuyu.job.upload;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.extra.ftp.FtpConfig;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.extra.ssh.Sftp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOCase;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.IOFileFilter;

import java.io.File;
import java.util.List;

/**
 * @author 紫水木鱼
 * @description 通过Email发送文件或邮件给指定地址
 * @company CHAOS
 * @date 2018/10/29 13:48
 */
@Slf4j
public class SendDataByEMailJob {


    public void sendEmail() {

        log.info("邮件发送文件任务已启动" + DateUtil.now());
        MailUtil.send("543254646@qq.com", "测试", "<h1>邮件来自Hutool测试</h1>", true);

    }

    public void sendFile() {
        log.info("邮件任务开始" + DateUtil.now());
        String localDir = "D:/Project/hunanbiwu";
        String powerDir = "D:/FilePath/SFTPPath/hunanbiwu";
        //String powerDir = localDir;
        DateTime today = DateUtil.date();
        if (!FileUtil.exist(localDir + "/" + DateUtil.formatDate(today))) {
            FileUtil.mkdir(localDir + "/" + DateUtil.formatDate(today));
        }
        DateTime startDay = DateUtil.offsetDay(today, 1);
        DateTime endDay = DateUtil.offsetDay(today, 10);
        IOFileFilter nameFileFilter = FileFilterUtils.suffixFileFilter(DateUtil.format(endDay, "yyyy_MM_dd") + ".xlsx", IOCase.INSENSITIVE);
        List<File> files = FileUtil.loopFiles(FileUtil.file(powerDir + "/PowerForecasting"), 2, nameFileFilter);
        if (files.size() == 39) {
            for (File file : files) {
                FileUtil.copy(file, FileUtil.file(localDir + "/" + DateUtil.formatDate(today)), true);
            }
        }
        //防止名称过被截断，在名字大于 60个字符的时候也会主动截断，避免中文文件名乱码
        System.getProperties().setProperty("mail.mime.splitlongparameters", "false");

        File zip = ZipUtil.zip(localDir + "/" + DateUtil.formatDate(today), localDir + "/北京木联能软件股份有限公司-" + DateUtil.formatDate(today) + ".zip", false);
        String title = "北京木联能软件股份有限公司-" + DateUtil.formatDate(today) + "预测数据";
        String emailContent = "附件内容为北京木联能软件股份有限公司对" + DateUtil.formatDate(startDay) + "到" + DateUtil.formatDate(endDay) + "之间的功率预测数据,请查收";
        //接收者
        String receiver = "dkyxnybw@163.com";
        //String receiver ="543254646@qq.com";
        MailUtil.send(receiver, title, emailContent, false, zip);
        log.info("预测数据已上传到湖南比武项目组邮箱-" + receiver + ":" + zip.getName());

    }

    public void sendYuanBaoFile() {
        log.info("邮件任务开始" + DateUtil.now());
        String powerDir = "D:\\FilePath\\SFTPPath\\mln\\PowerForecasting\\Yuanbao\\";
        String backPath = "D:\\FilePath\\localBackPath\\newDQForecast\\Yuanbao\\";
        DateTime today = DateUtil.date();
        backPath = backPath + DateUtil.formatDate(today);
        FileUtil.mkdir(backPath);
        FileUtil.mkdir(powerDir);
        //下载文件
        FtpConfig config = new FtpConfig("sftp.meteologica.asia", 2121, "mln", "5Qth_JX2", CharsetUtil.CHARSET_UTF_8);
        Sftp sftp = new Sftp(config);
        List<String> fileNames = sftp.ls("/PowerForecasting/Yuanbao");
        for (String fileName : fileNames) {
            sftp.download("/PowerForecasting/Yuanbao/" + fileName, FileUtil.file(powerDir, fileName));
            FileUtil.copy(FileUtil.file(powerDir, fileName), FileUtil.file(backPath, fileName), true);
            sftp.delFile("/PowerForecasting/Yuanbao/" + fileName);
        }
        IoUtil.close(sftp);
        //上传文件
        List<File> files = FileUtil.loopFiles(powerDir);
        if (CollUtil.isNotEmpty(files)) {
            MailAccount account = new MailAccount();
            account.setHost("smtp.qq.com");
            account.setPort(465);
            account.setSslEnable(true);
            account.setFrom("zishuimuyu@qq.com");
            account.setUser("zishuimuyu");
            account.setPass("muhwmipjxtwobfgh");
            //防止名称过被截断，在名字大于 60个字符的时候也会主动截断，避免中文文件名乱码
            System.getProperties().setProperty("mail.mime.splitlongparameters", "false");
            account.setSplitlongparameters(false);
            String fileTime = DateUtil.today();
            //Yuanbao_2024043006_power.csv
            for (File file : files) {
                ThreadUtil.safeSleep(2000);
                fileTime = DateUtil.formatDateTime(DateUtil.parse(StrUtil.subBetween(file.getName(), "_", "_"), "yyyyMMddHH"));
                String title = "北京木联能软件股份有限公司" + fileTime + "预测数据";
                String emailContent = "附件内容为北京木联能软件股份有限公司" + fileTime + "的功率预测数据,请查收";
                //接收者
                String receiver = "583493821@qq.com ";
                MailUtil.send(account, receiver, title, emailContent, false, file);
                FileUtil.del(file);

            }
        }

    }

}